package psn.hui.stat.utils;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFClientAnchor;
import org.apache.poi.hssf.usermodel.HSSFPatriarch;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.util.CellRangeAddress;

import psn.hui.bus.domain.Customer;
import psn.hui.bus.domain.Rent;

public class ExportRentUtils {
	private static SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
	/**
	 * 导出出租单
	 * 
	 * @param rent
	 * @param customer
	 * @param fileName
	 * @param sheetName
	 * @param response
	 */
	public static void exportRent(Rent rent, Customer customer, String fileName, String sheetName,
			HttpServletResponse response) {
		// 第一步.组装excel文档
		// 1.创建工作薄
		HSSFWorkbook workbook = new HSSFWorkbook();
		// 2.获取样式
		HSSFCellStyle titleCellStyle = ExportHSSFCellStyle.createTitleCellStyle(workbook);
		HSSFCellStyle baseCellStyle = ExportHSSFCellStyle.createTableRowCellStyle(workbook);
		// 3.创建sheet
		HSSFSheet sheet = workbook.createSheet(sheetName);
		// 4.设置sheet默认参数
		sheet.setDefaultColumnWidth(30);
		sheet.setColumnWidth(1, 50*256);
		// 5.合并标题行
		CellRangeAddress region_title = new CellRangeAddress(0, 0, 0, 3);
		sheet.addMergedRegion(region_title);
		// 6.创建标题行,设置样式，填入内容
		int row_index = 0;
		HSSFRow title_row = sheet.createRow(row_index++);
		HSSFCell title_cell = title_row.createCell(0);
		title_cell.setCellStyle(titleCellStyle);
		title_cell.setCellValue(customer.getCustname()+"-出租单信息");
		// 8.填充出租单内容
		//8.1 第二行
		HSSFRow row2 = sheet.createRow(row_index++);
		row2.setHeightInPoints(150);
		// 出租单号
		HSSFCell row2_cell0 = row2.createCell(0);
		row2_cell0.setCellStyle(baseCellStyle);
		row2_cell0.setCellValue("出租单号");
		HSSFCell row2_cell1 = row2.createCell(1);
		row2_cell1.setCellStyle(baseCellStyle);
		row2_cell1.setCellValue(rent.getRentid());
		// 二维码
		HSSFCell row2_cell2 = row2.createCell(2);
		row2_cell2.setCellStyle(baseCellStyle);
		row2_cell2.setCellValue("二维码");
		HSSFCell row2_cell3 = row2.createCell(3);
		row2_cell3.setCellStyle(baseCellStyle);
		//写入二维码 开始
		//获取二维码图片
		InputStream logoInputStream = ExportRentUtils.class.getClassLoader().getResourceAsStream("logo.jpg");
		BufferedImage codeimg = ZXingCodeEncodeUtils.createZXingCodeUseLogo(rent.getRentid(), 150, 150, logoInputStream);
		//图片写入流
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		try {
			ImageIO.write(codeimg, "JPEG", bos);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		//流写入数组
		byte[] byteArray = bos.toByteArray();
		//画图的顶级管理器，一个sheet只能获取一个
		HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
		/**
		 * 	参数4 设置图片的平铺程度 最大255
		 * 	参数5 开始画的单元格列
		 * 	参数6 开始画的单元格行
		 * 	参数7 结束画的单元格列
		 * 	参数8 结束画的单元格行
		 */
		HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 0, 255, (short)3, 1, (short)4, 1);
		anchor.setAnchorType(ClientAnchor.AnchorType.DONT_MOVE_AND_RESIZE);
		int pictureIndex = workbook.addPicture(byteArray, HSSFWorkbook.PICTURE_TYPE_JPEG);
		patriarch.createPicture(anchor, pictureIndex);
		
		//写入二维码 结束
		//8.2 第三行
		HSSFRow row3 = sheet.createRow(row_index++);
		//客户姓名
		HSSFCell row3_cell0 = row3.createCell(0);
		row3_cell0.setCellStyle(baseCellStyle);
		row3_cell0.setCellValue("客户姓名");
		HSSFCell row3_cell1 = row3.createCell(1);
		row3_cell1.setCellStyle(baseCellStyle);
		row3_cell1.setCellValue(customer.getCustname());
		//客户身份证
		HSSFCell row3_cell2 = row3.createCell(2);
		row3_cell2.setCellStyle(baseCellStyle);
		row3_cell2.setCellValue("客户身份证号");
		HSSFCell row3_cell3 = row3.createCell(3);
		row3_cell3.setCellStyle(baseCellStyle);
		row3_cell3.setCellValue(rent.getIdentity());
		//8.3 第四行
		HSSFRow row4 = sheet.createRow(row_index++);
		// 起租时间
		HSSFCell row4_cell0 = row4.createCell(0);
		row4_cell0.setCellStyle(baseCellStyle);
		row4_cell0.setCellValue("起租时间");
		HSSFCell row4_cell1 = row4.createCell(1);
		row4_cell1.setCellStyle(baseCellStyle);
		row4_cell1.setCellValue(dateFormat.format(rent.getBegindate()));
		//还车时间
		HSSFCell row4_cell2 = row4.createCell(2);
		row4_cell2.setCellStyle(baseCellStyle);
		row4_cell2.setCellValue("还车时间");
		HSSFCell row4_cell3 = row4.createCell(3);
		row4_cell3.setCellStyle(baseCellStyle);
		row4_cell3.setCellValue(dateFormat.format(rent.getReturntime()));
		//8.4 第五行
		HSSFRow row5 = sheet.createRow(row_index++);
		// 车牌号码
		HSSFCell row5_cell0 = row5.createCell(0);
		row5_cell0.setCellStyle(baseCellStyle);
		row5_cell0.setCellValue("车牌号码");
		HSSFCell row5_cell1 = row5.createCell(1);
		row5_cell1.setCellStyle(baseCellStyle);
		row5_cell1.setCellValue(rent.getCarnumber());
		// 出租价格
		HSSFCell row5_cell2 = row5.createCell(2);
		row5_cell2.setCellStyle(baseCellStyle);
		row5_cell2.setCellValue("出租价格");
		HSSFCell row5_cell3 = row5.createCell(3);
		row5_cell3.setCellStyle(baseCellStyle);
		row5_cell3.setCellValue(rent.getPrice());
		//8.5 第六行 空行
		row_index++;
		//8.6 第七行 
		HSSFRow row7 = sheet.createRow(row_index++);
		// 打印时间
		HSSFCell row7_cell2 = row7.createCell(2);
		row7_cell2.setCellStyle(baseCellStyle);
		row7_cell2.setCellValue("打印时间");
		HSSFCell row7_cell3 = row7.createCell(3);
		row7_cell3.setCellStyle(baseCellStyle);
		row7_cell3.setCellValue(dateFormat.format(new Date()));
		//8.7 第八行 
		HSSFRow row8 = sheet.createRow(row_index++);
		// 操作员
		HSSFCell row8_cell2 = row8.createCell(2);
		row8_cell2.setCellStyle(baseCellStyle);
		row8_cell2.setCellValue("操作员");
		HSSFCell row8_cell3 = row8.createCell(3);
		row8_cell3.setCellStyle(baseCellStyle);
		row8_cell3.setCellValue(rent.getOpername());
		//8.8 第九行 
		HSSFRow row9 = sheet.createRow(row_index++);
		// 客户签名
		HSSFCell row9_cell2 = row9.createCell(2);
		row9_cell2.setCellStyle(baseCellStyle);
		row9_cell2.setCellValue("客户签名");
		
		// 第二步.导出下载
		try {
			// 处理中文文件名编码
			fileName = URLEncoder.encode(fileName, "UTF-8");
			// 设置下载的文件的名称
			response.setContentType("application/vnd.ms-excel;charset=utf-8");
			response.setCharacterEncoding("UTF-8");
			response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
			workbook.write(response.getOutputStream());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
